// 处理排序
// 收藏的线路置顶展示;无收藏线路，站点距离小排前面，若线路显示暂无车辆信息，以线路名由小到大排序
function sortList(list) {
  let sortList = []
  let collectList = [] // 收藏数组
  let nowBusStopList = [] // 站点距离数组
  let busNameList = [] // 暂无车辆信息数组
  list.map((item, index) => {
    if (
      (item.upBusesInfoModel && item.upBusesInfoModel.isCollect) ||
      (item.downBusesInfoModel && item.downBusesInfoModel.isCollect)
    ) {
      collectList.push(item)
    } else {
      let itemModel = disposeSort(item)
      if (itemModel.nowBusStop !== -1) {
        nowBusStopList.push(item)
      } else {
        busNameList.push(item)
      }
    }
  })
  // 站点距离排序
  nowBusStopList.sort((next, cur) => {
    let nextItemModel = disposeSort(next)
    let curItemModel = disposeSort(cur)
    if (nextItemModel.nowBusStop === -99) {
      return -1
    } else if (curItemModel.nowBusStop === -99) {
      return 1
    } else {
      return nextItemModel.nowBusStop - curItemModel.nowBusStop
    }
  })
  // 暂无车辆信息排序
  busNameList.sort((next, cur) => {
    let nextItemModel = disposeSort(next)
    let curItemModel = disposeSort(cur)
    if (
      !isNaN(parseInt(nextItemModel.busName)) &&
      !isNaN(parseInt(curItemModel.busName))
    ) {
      return parseInt(nextItemModel.busName) - parseInt(curItemModel.busName)
    } else {
      return curItemModel.busName.localeCompare(nextItemModel.busName)
    }
  })
  sortList = sortList.concat(collectList, nowBusStopList, busNameList)
  return sortList
}

function disposeSort(itemModel) {
  return itemModel.upBusesInfoModel
    ? itemModel.upBusesInfoModel
    : itemModel.downBusesInfoModel
}

export { sortList }
